package com.qingyun.form.items

import android.app.DatePickerDialog
import android.view.View
import com.qingyun.form.R
import com.qingyun.form.base.FormType
import com.qingyun.form.base.FormViewHolder
import com.qingyun.form.dialog.DatePickDialogFragment
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale

class DateFormItem(
    label: String,
    name: String,
    value: String?,
    required: Boolean = false,
    dateFormat: String? = null,
) : BaseFormItem(label, name, value) {
    override val formType: FormType
        get() = FormType.Date
    override val layoutId: Int
        get() = R.layout.form_item_date

    private var format = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())

    override var hint: String? = null
        get() = if (field.isNullOrEmpty()) "请选择" else field

    /** 日期格式  */
    var dateFormat: String = "yyyy-MM-dd"
        set(value) {
            field = value
            this.format = SimpleDateFormat(value, Locale.getDefault())
        }

    /** 开始日期 */
    var startDate: String? = null

    /** 结束日期 */
    var endDate: String? = null

    /** 当前日期 */
    private var currentDate: Date? = null
    private val datePickListener =
        DatePickerDialog.OnDateSetListener { _, year, month, dayOfMonth ->
            val calendar = Calendar.getInstance()
            calendar.set(year, month, dayOfMonth)
            this.onDateChanged(calendar.time)
        }

    private fun onDateChanged(date: Date) {
        currentDate = date
        val value = format.format(date)
        onValueChanged(value, true)
    }

    init {
        dateFormat?.let {
            this.dateFormat = it
        }
        this.currentDate = value?.let { format.parse(it) }
        this.required = required
    }

    override fun onBindViewHolder(holder: FormViewHolder) {
        holder.updateTitleLayout()
        holder.setHint(R.id.tvDate, this.hint, this.viewOnly)
        holder.setText(R.id.tvDate, this.value)
    }

    override fun onBindViewEvent(holder: FormViewHolder) {
        holder.itemView.setOnClickListener(this::onItemClick)
    }

    override fun onUnBindViewEvent(holder: FormViewHolder) {
        holder.itemView.setOnClickListener(null)
    }

    override fun onLayoutChildren(holder: FormViewHolder) {
        holder.updateTitleLayout()
    }

    override fun getTitleLayoutWidth(holder: FormViewHolder): Int {
        return holder.findTitleLayout().measuredWidth
    }

    private fun onItemClick(view: View) {
        showDatePicker()
    }

    private fun showDatePicker() {
        val manager = fragmentManager ?: return
        DatePickDialogFragment()
            .setDate(currentDate)
            .apply {
                this.minDate = startDate?.let { format.parse(it)?.time }
                this.maxDate = endDate?.let { format.parse(it)?.time }
                this.listener = datePickListener
            }
            .show(manager, "DatePickDialogFragment")
    }
}